package com.beijing.demo.queue;

/**
 * 曹旭
 * 2025/5/7
 * 设计循环队列
 * https://leetcode.cn/problems/design-circular-queue/
 */
public class MyCircularQueue {
    public int[] array;
    public int size;
    public int l;
    public int r;
    public int limit;
    public MyCircularQueue(int k) {
        array = new int[k];
        size = 0;
        l = r = 0;
        limit=k;
    }

    public boolean enQueue(int value) {
        if(isFull()){
            return false;
        }else {
            r = r == limit ? 0 : r;
            array[r++] = value;
            size++;
        }
        return true;
    }

    public boolean deQueue() {
        if(isEmpty()){
            return false;
        } else {
            l = l == limit - 1 ?  0 : ++l;
            size--;
        }
        return true;
    }

    public int Front() {
        if(isEmpty()){
            return -1;
        } else {
            return array[l];
        }
    }

    public int Rear() {
        if(isEmpty()){
            return -1;
        } else {
            return array[r - 1];
        }
    }

    public boolean isEmpty() {
        return size == 0;
    }

    public boolean isFull() {
        return size == limit;
    }

    public static void main(String[] args) {
//        ["MyCircularQueue","enQueue","enQueue","enQueue","enQueue","deQueue","deQueue","isEmpty","isEmpty","Rear","Rear","deQueue"]
//[[8],  [3],[9],[5],[0],[],[],[],[],[],[],[]]
//[null,true,true,true,true,true,true,false,false,3,3,true]
//[null,true,true,true,true,true,true,false,false,0,0,true]


// ["MyCircularQueue","enQueue","Rear","enQueue","deQueue", "Front"  ,"deQueue","deQueue","isEmpty","deQueue","enQueue","enQueue"]
// [[2],   [4],[],[9],[],[],[],[],[],[],[6],[4]]
// [null,true,4,true,true,4,true,false,true,false,true,true]
// [null,true,4,true,true,9,true,false,true,false,true,true]
        MyCircularQueue queue = new MyCircularQueue(2);
        queue.enQueue(4);
        queue.enQueue(9);
//        queue.enQueue(5);
//        queue.enQueue(0);
//        queue.deQueue();
        queue.deQueue();
        queue.Front();
    }
}
